package com.ruoyi.wechat.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageUtil {
	private static XStream xstream = new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				boolean cdata = true;

				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
				}

				protected void writeText(QuickWriter writer, String text) {
					if (this.cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}
	});

	/*public static String textMessageToXml(TextMessage textMessage) {
		xstream.alias("xml", textMessage.getClass());
		return xstream.toXML(textMessage);
	}

	public static String musicMessageToXml(MusicMessage musicMessage) {
		xstream.alias("xml", musicMessage.getClass());
		return xstream.toXML(musicMessage);
	}

	public static String newsMessageToXml(NewsMessage newsMessage) {
		xstream.alias("xml", newsMessage.getClass());
		xstream.alias("item", new Article().getClass());
		return xstream.toXML(newsMessage);
	}

	public static String messageToXml(BaseMessage message) {
		if ((message instanceof TextMessage)) {
			TextMessage textmessage = (TextMessage) message;
			return textMessageToXml(textmessage);
		}
		if ((message instanceof NewsMessage)) {
			NewsMessage newsmessage = (NewsMessage) message;
			return newsMessageToXml(newsmessage);
		}
		return null;
	}

	public static BaseMessage createTextMessage(Map<String, String> requestMap, String msg) {
		String fromUserName = (String) requestMap.get("FromUserName");
		String toUserName = (String) requestMap.get("ToUserName");
		String msgType = (String) requestMap.get("MsgType");

		TextMessage textMessage = new TextMessage();
		textMessage.setToUserName(fromUserName);
		textMessage.setFromUserName(toUserName);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType("text");
		textMessage.setContent(msg);
		return textMessage;
	}*/

/*	public static String beanToXml(Object obj) {
		xstream.alias("xml", obj.getClass());
		return xstream.toXML(obj);
	}*/
	 public static Map<String, String> parseXml(HttpServletRequest request)
		    throws Exception
		  {
		    Map map = new HashMap();

		    InputStream inputStream = request.getInputStream();

		    SAXReader reader = new SAXReader();
		    Document document = reader.read(inputStream);

		    Element root = document.getRootElement();

		    List<Element> elementList = root.elements();

		    for (Element e : elementList) {
		      map.put(e.getName(), e.getText());
		    }

		    inputStream.close();
		    inputStream = null;

		    return map;
		  }
}
